package com.fessc.develop.handler;

import com.fessc.develop.entity.dbvo.TPayment;
import com.fessc.develop.exception.BusinessException;
import com.fessc.develop.handler.processor.send.ISendProcessor;
import com.fessc.develop.model.enums.EnumBranch;
import com.fessc.develop.model.enums.EnumPushType;
import com.fessc.develop.utils.BusiUtil;
import com.fessc.develop.utils.ExceptionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class PrePaymentRefundPushHandler {
    @Resource
    private List<ISendProcessor> refundProcessors;

    private Map<String, ISendProcessor> processorMap;

    @PostConstruct
    public void init() {
        if (BusiUtil.isNotNull(refundProcessors)) {
            processorMap = refundProcessors.stream().collect(Collectors.toMap(ISendProcessor::getProcessorForBranch, p->p, (v1, v2) -> v1));
        } else {
            processorMap = new HashMap<>();
        }
    }

    public void refundPush(List<TPayment> paymentList, String pushType) {
        //按照银行分类处理
        Map<String, List<TPayment>> paymentMap = paymentList.stream().collect(Collectors.groupingBy(t -> t.getBankid() + t.getRefundBatchNo()));

        //加工构造推送报文，调用接口推送
        refundProcess(paymentMap);

    }

    private void refundProcess(Map<String, List<TPayment>> paymentMap) {
        for (Map.Entry<String, List<TPayment>> entry : paymentMap.entrySet())  {
            try {
                ISendProcessor processor = BusiUtil.isNull(processorMap.get(entry.getKey()))? processorMap.get(EnumBranch.COMMON.toString()) : processorMap.get(entry.getKey().toString());
                if (BusiUtil.isNull(processor)) {
                    throw ExceptionUtil.businessException("EE4001");
                }
                processor.prePaymentRefundProcess(entry.getValue()); //预付款退款信息推送
            } catch (Exception e) {
                if (e instanceof BusinessException) {
                    log.error(((BusinessException) e).getResult().getMsg(), e);
                } else {
                    log.error(e.getMessage(), e);
                }
            }
        }
    }

    /**
     * 预付款退款交易状态查询推送
     */
    public void prePaymentRefundStatusPush(List<TPayment> paymentList, String pushType) {
        //按照银行分类处理
        Map<String, List<TPayment>> paymentMap = paymentList.stream().collect(Collectors.groupingBy(t -> t.getBankid() + t.getRefundBatchNo()));

        //加工构造推送报文，调用接口推送
        prePaymentRefundStatusProcess(paymentMap);

    }

    private void prePaymentRefundStatusProcess(Map<String, List<TPayment>> paymentMap) {
        for (Map.Entry<String, List<TPayment>> entry : paymentMap.entrySet())  {
            try {
                ISendProcessor processor = BusiUtil.isNull(processorMap.get(entry.getKey()))? processorMap.get(EnumBranch.COMMON.toString()) : processorMap.get(entry.getKey());
                if (BusiUtil.isNull(processor)) {
                    throw ExceptionUtil.businessException("EE4001");
                }
                processor.prePaymentRefundStatusProcess(entry.getValue()); //预付款交易状态查询推送
            } catch (Exception e) {
                if (e instanceof BusinessException) {
                    log.error(((BusinessException) e).getResult().getMsg(), e);
                } else {
                    log.error(e.getMessage(), e);
                }
            }
        }
    }

}
